# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""
from jili.core.printlog import print

def add_1(n0,n1):
    n=n0+1
    if n==n1:
        return 0,1
    else:
        return n,0
def add_combine_1(n0,n1):
    a,b=add_1(n0[-1],n1[-1])
    c0=n0[:-1]
    c1=n1[:-1]
    if b!=0:
        c0=add_combine_1(c0,c1)
    c0.append(a)
    return c0
def isend(n0,n1):
    a=False
    if n0[0]+1==n1[0]:
        if n0[1:]:
            a=isend(n0[1:],n1[1:])
        else:
            a=True
    return a
def allpairs(argss,filter_func=None,filter_funcbylist=None,keys=None,add_data={}):
    if isinstance(argss,dict):
        keys=list(argss.keys())
        args=list(argss.values())
    else:
        args=argss
    n1=[]
    n0=[]
    for i in args:
        n1.append(len(i))
        n0.append(0)
    while not isend(n0,n1):
        rst=[]
        i=0
        for j in n0:
            rst.append(args[i][j])
            i=i+1
        if filter_funcbylist is not None:
            check1=filter_funcbylist(rst)
        else:
            check1=True
        if keys and check1:
            rst = dict(zip(keys, rst))
            if add_data:
                rst.update(add_data)
        if filter_func is not None:
            check2=filter_func(rst)
        else:
            check2=True
        if check1 and check2:
            yield rst
        n0=add_combine_1(n0,n1)
    rst=[]
    i=0
    for j in n0:
        rst.append(args[i][j])
        i=i+1
    if filter_funcbylist   is not None:
        check1 = filter_funcbylist(rst)
    else:
        check1=True
    if check1 and keys:
        rst = dict(zip(keys, rst))
        if add_data:
            rst.update(add_data)
    if filter_func   is not None:
        check2 = filter_func(rst)
    else:
        check2 = True
    if check1 and check2:
        yield rst

if __name__=="__main__":
    def filter0(x):
        if x[0]=="TA" and x[1]=="k1m":
            return False
        else:
            return True
    
    for i in allpairs([["TA","rb","fu"],["k1m","k3m"],[3,2,1]],filter_func=filter0):
        print(i)
    
    a=[[1,2,3],[1,2]]
    n1=[]
    n0=[]
    for i in a:
        n1.append(len(i))
        n0.append(0)
    add_combine_1(n0,n1)